<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="" prop="member_unit_name">
            <el-input v-model="queryParams.member_unit_name" placeholder="单位名称" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="" prop="phone">
            <el-input v-model="queryParams.phone" placeholder="联系人/电话" clearable style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="" prop="status">
            <el-select v-model="queryParams.status" placeholder="单位会员等级" clearable style="width: 240px">
              <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['system:user:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['association:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getUnitList" :columns="columns"></right-toolbar>
      <div>
      </div>
    </el-row>



    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--单位会员数据-->
        <pane size="84">
          <el-col>
            <el-table v-loading="loading" :data="unitList" @selection-change="">
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column label="LOGO" align="center" key="member_logo" prop="member_logo"
                v-if="columns[0].visible" />
              <el-table-column label="会员单位名称" align="center" key="member_unit_name" prop="member_unit_name"
                :show-overflow-tooltip="true" v-if="columns[1].visible" />
              <el-table-column label="会员单位等级" align="center" key="member_grade" prop="member_grade"
                v-if="columns[2].visible" :show-overflow-tooltip="true" />
              <el-table-column label="联系人" align="center" key="contact" prop="contact" :show-overflow-tooltip="true"
                v-if="columns[3].visible" />
              <el-table-column label="联系电话" align="center" key="phone" prop="phone" width="120"
                v-if="columns[4].visible" />
              <el-table-column label="职位" align="center" key="position" prop="position" v-if="columns[5].visible" />
              <el-table-column label="到期时间" align="center" key="maturity_time" prop="maturity_time"
                v-if="columns[6].visible" />
              <el-table-column label="入会时间" align="center" key="create_time" prop="create_time"
                v-if="columns[7].visible" />
              <el-table-column label="单位成员" align="center" key="member_count" prop="member_count" />
              <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
                <template slot-scope="scope" v-if="scope.row.userId !== 1">
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                    v-hasPermi="['association:unitupdate']">修改</el-button>
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)"
                    v-hasPermi="['association:unitdetailt']">详情</el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete" @click="handleToStatus(scope.row)"
                    v-hasPermi="['association:unitstatus']">
                    {{ scope.row.status == 2 ? "正常" : "注销" }}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize" @pagination="" />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="会员单位logo" prop="member_logo">
          <el-input v-model="form.member_logo" placeholder="请输入会员单位logo" :disabled="detail" />
        </el-form-item>
        <el-form-item label="会员单位名称" prop="member_unit_name">
          <el-input v-model="form.member_unit_name" placeholder="请输入会员单位名称" :disabled="detail" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" :disabled="detail" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" :disabled="detail" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" placeholder="请输入职位" :disabled="detail" />
        </el-form-item>
        <el-form-item label="到期时间" prop="maturity_time">
          <el-date-picker clearable v-model="form.maturity_time" :disabled="detail" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择到期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="detail" >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";
import { updateStatus,updateUnit,unitdetail, listUnit } from "@/api/unit/unit";
import { getToken } from "@/utils/auth";
export default {
  name: "member",
  dicts: [],
  components: { Treeselect, Splitpanes, Pane },
  data() {
    return {
      columns: [
        { key: 0, label: `LOGO`, visible: true },
        { key: 1, label: `会员单位名称`, visible: true },
        { key: 2, label: `会员单位等级`, visible: true },
        { key: 3, label: `联系人`, visible: true },
        { key: 4, label: `联系电话`, visible: true },
        { key: 5, label: `到期时间`, visible: true },
        { key: 6, label: `入会时间`, visible: true },
        { key: 7, label: `单位成员`, visible: true },
      ],
      // 会员单位导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的会员单位数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/association/importData"
      },
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      detail:false,
      // 单位会员表格数据
      unitList: null,
      // 表单参数
      form: {},
      //单位状态
      status:'',
      // 表单校验
      rules: {
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        member_unit_name: undefined,
        phone: undefined,
      },
    }
  },
  methods: {
    /** 查询用户列表 */
    getUnitList() {
      this.loading = true;
      listUnit(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.unitList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detail = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        memberLogo: null,
        memberUnitName: null,
        memberGrade: null,
        contact: null,
        phone: null,
        position: null,
        maturityTime: null,
        createTime: null,
        updateTime: null,
        deleteTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getUnitList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/association/export', {
        ...this.queryParams
      }, `会员单位_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "会员单位导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/association/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      unitdetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员单位";
      });
    },
    handleDetail(row) {
      this.detail = true;
      const id = row.id || this.ids;
      unitdetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "会员单位详情";
      });
    },
    handleToStatus(row) {
      if(row.status==1){
        row.status=2;
      }else{
        row.status=1;
      }
      updateStatus(row).then(response => {
        this.getUnitList();
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUnit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.detail = false;
              this.getUnitList();
            });
          }
        }
      });
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getUnitList();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
  },
  created() {
    this.getUnitList();
  },
}
</script>
<style scoped>
.box1 {
  padding-top: 3px;
}
</style>